package com.oracle.lingshangejia.utils;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Hang.Yang on 2018/6/15 10:38.
 * If you find a path with no obstacles, it probably doesn’t lead anywhere.
 * In LongCaiHaErBin
 */

public class SaveImageToGallery {

    public static void saveImageToGallery(Bitmap bmp){

        // 首先保存图片
        String fileName = System.currentTimeMillis() + ".png";
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), fileName);

        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bmp!=null) {
                bmp.recycle();
            }
        }

        /*//其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(MyApp.INSTANCE.currentActivity().getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
            UtilToast.show("保存成功");
        } catch (FileNotFoundException e) {
            UtilToast.show("保存失败");
            e.printStackTrace();
        }*/

//        // 最后通知图库更新
//        MyApp.INSTANCE.currentActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(file.getPath()))));
//
//        UtilToast.show("保存成功");
    }
}
